package com.pureut.order.controller;

import com.pureut.common.core.domain.R;
import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.order.dto.SalesOrderDetailDto;
import com.pureut.order.service.SalesOrderDetailService;
import com.pureut.order.vo.GenerateInvoiceVo;
import com.pureut.order.vo.OrderReviewVo;
import com.pureut.order.vo.SalesOrderDetailVo;
import com.pureut.system.api.vo.PickingSaleVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/***
 * 销售明细列表
 * Author:C
 * Date:2022/12/01 14:59
 * @DESC
 */
@RestController
@RequestMapping("/sales-order-detail")
public class SalesOrderDetailController extends BaseController {


    @Resource
    SalesOrderDetailService salesOrderDetailService;


    /**
     * 获取销售订单明细列表
     *
     * @param id
     * @return
     */
    @GetMapping("/list/{id}")
    public TableDataInfo list(@PathVariable("id") Long id) {
        startPage();
        //查询销售订单列表
        List<SalesOrderDetailDto> list = salesOrderDetailService.getList(id);
        return getDataTable(list);
    }

    /**
     * 查看明细
     */
    @GetMapping("/view/{id}")
    public AjaxResult view(@PathVariable("id") Long id) {
        return AjaxResult.success(salesOrderDetailService.getView(id));
    }

    /**
     * 修改
     */
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody SalesOrderDetailVo salesOrderDetailVo) {
        return toAjax(salesOrderDetailService.updateSalesOrderDetail(salesOrderDetailVo));
    }

    /**
     * 根据订单id和物料编码查询销售明细
     */
    @GetMapping("/getDetailById")
    public AjaxResult getDetailById(Long salesOrderId, String materialCode) {
        return AjaxResult.success(salesOrderDetailService.getDetailById(salesOrderId, materialCode));
    }


    /**
     * 生成发货单
     */
    @RequiresPermissions("order:sale:saleOrder:invoice")
    @PostMapping("/generateInvoice")
    @Log(title = "生成发货单", businessType = BusinessType.INSERT)
    public AjaxResult generateInvoice(@RequestBody GenerateInvoiceVo generateInvoiceVo) {
        return AjaxResult.success(salesOrderDetailService.generateInvoice(generateInvoiceVo));
    }

    /**
     * 根据物料编码获取销售订单
     */
    @GetMapping("/getOrderByMaterialCode")
    public AjaxResult getOrderByMaterialCode(String materialCode) {
        return AjaxResult.success(salesOrderDetailService.getOrderByMaterialCode(materialCode));
    }


}
